package view_body

import (
	"PackingScanApp/models"

	"github.com/dtylman/gowd"
)

var (
	html_Data_str = models.Html_Data_str{
		SystemMsg:         `<b style="color:red">--</b>`,
		PackingProducts:   `--`,
		PackingInputUser:  `--`,
		FBillNo:           `--`,
		FNumber:           `--`,
		PackingNumHTML:    `--`,
		NowPackingNumHTML: `--`,
		NowScanData:       `--`,
		Line_mark:         `--`,
		Line_mark_note:    `--`,
		PackingListHTML:   ``,
	}

	//通讯管道
	bodyCChan = bodyCommunicationChan{
		DoShowBody: make(chan int, 1),
	}
)

type bodyCommunicationChan struct {
	DoShowBody chan int
}
type body_S struct {
	em gowd.ElementsMap
}

func GetHtmlBody() *models.Html_Data_str {
	return &html_Data_str
}

//向外提供通讯管道
func GetBodyCChan() *bodyCommunicationChan {
	return &bodyCChan
}

func NewBody() {

	body, err := gowd.ParseElement(`<div class="card-body"></div>`, nil)
	if err != nil {
		//panic(err)
	}
	b := body_S{
		em: gowd.NewElementMap(),
	}
	html_str := setBodyHTML(GetHtmlBody(), 1)
	body.AddHTML(html_str, b.em)

	go b.goSetHtml()

	gowd.Run(body)

}

func setBodyHTML(body_html *models.Html_Data_str, stype int) string {
	str1 := ``
	str2 := ``
	if stype == 1 {
		str1 = `<div class="table-responsive" id="divId">`
		str2 = `</div>`
	}
	html_str := str1 + `<table class="table table-bordered">   
              <tbody>
                <tr>
                  <td colspan="7"><b>当前装箱信息</b></td>   
                </tr>
                
                <tr>
                  <td>装配产品：<b>` + html_Data_str.PackingProducts + `</b></td>
                  <td>装配人员：<b>` + html_Data_str.PackingInputUser + `</b></td>
                  <td>产线标识：<b>` + html_Data_str.Line_mark + `</b></td>
                  <td>产线标识备注：<b>` + html_Data_str.Line_mark_note + `</b></td>
                  <td>流程卡号：<b>` + html_Data_str.FBillNo + `</b></td>
                  <td>物料ID：<b>` + html_Data_str.FNumber + `</b></td>                 
                  <td style="text-align:center">尾箱打印</td>  
                </tr>
                <tr>
                  <td colspan="6">提示消息：` + html_Data_str.SystemMsg + `</td> 
                  <td rowspan="2" style="text-align:center"><img src="scan_finish.png"/></td>   
                </tr>
                <tr>
                  <td colspan="4">最新扫描枪内容：<b>` + html_Data_str.NowScanData + `</b></td> 
                  <td>当前物料需装箱数量：<b>` + html_Data_str.PackingNumHTML + `</b></td>
                  <td>已装箱数量：<b style="color:red;" >` + html_Data_str.NowPackingNumHTML + `</b></td>    
                </tr>
              </tbody>
            </table>
            
            <table class="table table-bordered">
              <thead>
                <tr>
                  <th>已装箱产品条码</th>
                  <th>装箱时间</th>
                </tr>
              </thead>
              <tbody>
                ` + html_Data_str.PackingListHTML + `
              </tbody>
            </table>` + str2

	return html_str
}

func (b *body_S) goSetHtml() {
	for {
		select {
		case <-bodyCChan.DoShowBody:
			b.reSetHTML()
		}
	}
}

func (b *body_S) reSetHTML() {
	b.em["divId"].SetText("")
	html_str := setBodyHTML(GetHtmlBody(), 0)
	b.em["divId"].AddHTML(html_str, nil)
	b.em["divId"].Render()
}
